//package com.study.quiz.advice;
//
//import com.alibaba.fastjson.JSON;
//import com.study.quiz.dto.ResultDTO;
//import com.study.quiz.enums.ResultMsgEnum;
//import com.study.quiz.exception.QuizException;
//import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//
///**
// * @author CJunGod
// * @create 2020-03-08 14:39
// **/
//@ControllerAdvice
//public class QuizExceptionHandler {
//
//    @ExceptionHandler(Exception.class)
//    ModelAndView handleControllerException(HttpServletRequest request, Throwable e, Model model,
//                                     HttpServletResponse response) {
//        String contentType = request.getContentType();
//        if("application/json".equals(contentType)){
//            ResultDTO resultDTO;
//            if(e instanceof QuizException){
//                resultDTO = ResultDTO.errorOf((QuizException)e);
//            }else {
//                resultDTO =  ResultDTO.indexOfMsg(ResultMsgEnum.SYSTEM_ERROR);
//            }
//            try{
//                response.setStatus(200);
//                response.setCharacterEncoding("UTF-8");
//                response.setContentType("application/json");
//                PrintWriter writer = response.getWriter();
//                writer.write(JSON.toJSONString(resultDTO));
//                writer.close();
//            }catch (IOException io){
//
//            }
//            return null;
//        }else {
//            if(e instanceof QuizException){
//                model.addAttribute("message", e.getMessage());
//            }else {
//                model.addAttribute("message", ResultMsgEnum.SYSTEM_ERROR.getMessage());
//            }
//        }
//
//        return new ModelAndView("/common/error");
//    }
//
//}
